package com.eling.elcms.travel.webapp.controller;

import com.eling.elcms.basedoc.model.view.RoomStatisticsView;
import com.eling.elcms.core.dao.hibernate.search.SearchCondition;
import com.eling.elcms.core.exception.BusinessException;
import com.eling.elcms.core.util.PropertyUtils;
import com.eling.elcms.core.webapp.controller.BaseFormController;
import com.eling.elcms.core.webapp.controller.Msg;
import com.eling.elcms.core.webapp.spring.ModelFilling;
import com.eling.elcms.travel.model.CheckInCar;
import com.eling.elcms.travel.model.CheckInMeeting;
import com.eling.elcms.travel.model.ShiftChange;
import com.eling.elcms.travel.model.view.ChargeTotalView;
import com.eling.elcms.travel.model.view.CheckInCountView;
import com.eling.elcms.travel.model.view.MemberCountView;
import com.eling.elcms.travel.service.IShiftChangeManager;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Controller
public class ShiftChangeController extends BaseFormController{

	@Autowired
	private IShiftChangeManager shiftChangeManager;
	
	@RequestMapping("/api/shiftchange/save")
    @ResponseBody
	public ShiftChange save(@ModelAttribute@ModelFilling ShiftChange shiftchange){
		Date now = new Date();
		if(shiftchange.getPkShiftChange()==null){
			List<ShiftChange> list  = new ArrayList<ShiftChange>();
			ShiftChange sc = new ShiftChange();
			sc.setStartWorkDate(DateUtils.truncate(new  Date(), Calendar.DATE));
			PropertyUtils.setProperty(sc, "startWorkDateEnd",DateUtils.addDays(DateUtils.truncate(new  Date(), Calendar.DATE), 1));
			list=query(sc);	
			if(list.size()>2){
				throw new BusinessException("当天已接班!");
			}
		}
		if(shiftchange.getDayShiftAttention()!=null){
			if(shiftchange.getDayShiftAttention().length()>1023){
				throw new BusinessException("次日班注意事项，您输入的字数过多!");
			}
		}
		if(shiftchange.getMatters()!=null){
			if(shiftchange.getMatters().length()>1023){
				throw new BusinessException("周知事宜，您输入的字数过多!");
			}
		}
		if(shiftchange.getNightShiftAttention()!=null){
			if(shiftchange.getNightShiftAttention().length()>1023){
				throw new BusinessException("夜班注意事项，您输入的字数过多!");
			}
		}
		if(shiftchange.getExchangeGoods()!=null){
			if(shiftchange.getExchangeGoods().length()>1023){
				throw new BusinessException("物资交接，您输入的字数过多!");
			}
		}
		
		if (shiftchange.getPkShiftChange() != null) {
			if (!DateUtils.isSameDay(now, shiftchange.getStartWorkDate())) {
				throw new BusinessException("接班日期与当前日期不符！");
			}
			// 设置交班时间
			shiftchange.setFinishWorkDate(now);
		} else {
			// 设置接班时间
			shiftchange.setStartWorkDate(now);
		}
		return shiftChangeManager.save(shiftchange);
	}
	
	@RequestMapping("/api/shiftchange/{pkShiftChange}/delete")
	@ResponseBody
	public Msg delete(@PathVariable Long pkShiftChange){
		shiftChangeManager.remove(pkShiftChange);
		return new Msg("删除成功");
	}
	
	@RequestMapping("/api/shiftchange/query")
    @ResponseBody
	public List<ShiftChange> query(@ModelAttribute ShiftChange cond){
		List<ShiftChange> query = shiftChangeManager.query(cond);
		if(query == null || query.isEmpty()){
			return query;
		}
		//只显示最后一条数据的信息
		List<ShiftChange> returnList = new ArrayList<>();
		returnList.add(query.get(query.size()-1));
		return returnList;
	}
	
	@RequestMapping("/api/shiftchange/search*")
	@ResponseBody
	public List<ShiftChange> search(@ModelAttribute SearchCondition sc,@ModelAttribute ShiftChange cond){
		sc.setQueryCondition(cond);
		return shiftChangeManager.search(sc);
	}
	
	//房间入住数量
	@RequestMapping("/api/shiftchange/querycheckincount")
	@ResponseBody
	public List<CheckInCountView> queryCheckInCount(@RequestParam(value="shiftChangeDate", required = false) Date shiftChangeDate){
		return shiftChangeManager.queryCheckInCount(shiftChangeDate);
	}
	
	//会议
	@RequestMapping("/api/shiftchange/querymeeting")
	@ResponseBody
	public List<CheckInMeeting> queryMeeting(@RequestParam(value = "createDate",required = false) Date createDate,
			@RequestParam(value = "createEndDate",required = false) Date createEndDate){
		return shiftChangeManager.queryMeeting(createDate,createEndDate);
	}
	
	//派车
	@RequestMapping("/api/shiftchange/querycar")
	@ResponseBody
	public List<CheckInCar> queryCar(@RequestParam(value = "createDate",required = false) Date createDate,
			@RequestParam(value = "createEndDate",required = false) Date createEndDate){
		return shiftChangeManager.queryCar(createDate,createEndDate);
	}
	
	//房态
	@RequestMapping("/api/shiftchange/statistics")
    @ResponseBody
    public List<RoomStatisticsView> queryStatistics(@RequestParam(value = "shiftChangeDate",required = false) Date shiftChangeDate){
    	return shiftChangeManager.queryStatistics(shiftChangeDate); 
    }
	
	//查询会员
	@RequestMapping("/api/shiftchange/querymember")
	@ResponseBody
	public List<MemberCountView> queryMember(@RequestParam(value = "shiftChangeDate",required = false) Date shiftChangeDate){
		return shiftChangeManager.queryMember(shiftChangeDate);
	}
	
	//查询款项
	@RequestMapping("/api/shiftchange/querycharge")
	@ResponseBody
	public List<ChargeTotalView> queryCharge(@RequestParam(value = "shiftChangeDate",required = false) Date shiftChangeDate){
		return shiftChangeManager.queryCharge(shiftChangeDate);
	}
}
